#include "pugiconfig.hpp"
#include "pugixml.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdio.h>


typedef std::vector<int> Vec;
typedef std::vector<Vec> Mat;

void crearArchivoXML()
{
    pugi::xml_document doc;

    //[code_modify_add
    // add node with some name
    pugi::xml_node node = doc.append_child();
    node.set_name("node");

    // add description node with text child
    pugi::xml_node descr = node.append_child();
    descr.set_name("description");
    descr.append_child(pugi::node_pcdata).set_value("Simple node");

    // add param node before the description
    pugi::xml_node param = node.insert_child_before(pugi::node_element, descr);
    param.set_name("param");

    // add attributes to param node
    param.append_attribute("name") = "version";
    param.append_attribute("value") = 1.1;
    param.insert_attribute_after("type", param.attribute("name")) = "float";
    //]

    doc.print(std::cout);
}

void cargarArchivoXML(){
pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file("prueba.xml");
    std::cout << "Load result: " << result.description() << std::endl;
    pugi::xpath_node_set tools = doc.select_nodes("/juego/campania//*");
    Vec miVect;
    Mat miMatriz;
    std::string fila;
    int i =0;
        int j=0;

    std::cout << "Contenido de la campania" << std::endl;

    for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) {
        pugi::xpath_node node = *it;
        std::cout << "campo: " << node.node().name() << ", valor: " <<
                node.node().child_value() << std::endl;
    }
    std::cout << std::endl;
    std::cout << "guardo el mapa" << std::endl;
    tools = doc.select_nodes("/juego/campania/mapa_nivel//*");
    for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) {
        pugi::xpath_node node = *it;
        //extraigo los datos de una fila
        fila=node.node().child_value();
        std::istringstream in(fila);
        //guardo los datos en un vector de enteros
        std::copy(std::istream_iterator<int>(in),
                  std::istream_iterator<int>(),
                  std::back_inserter(miVect));
        miMatriz.push_back(miVect);
        miVect.clear();
    }
    std::cout << std::endl;
    std::cout << "imprimo la matriz" << std::endl;
    for (std::vector<Vec>::iterator it = miMatriz.begin(); it != miMatriz.end(); ++it){
        miVect=*it;
        ++i;
        j=1;
        for (std::vector<int>::iterator it2 = miVect.begin(); it2 != miVect.end(); ++it2) {
            printf("Fila:%d Columna:%d Valor:%d\n",i,j,*it2);
            ++j;
        }
    }
    std::cout << '\n';
}

void GenerarMap(std::string seccion, std::string pathSalida) {
    pugi::xpath_node_set tools = this->doc.select_nodes(seccion.c_str());
    std::string fila;
    std::ofstream salida; std::stringstream convert;
    salida.open(pathSalida.c_str(), fstream::out);

    for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) {
        pugi::xpath_node node = *it;
        std::vector<std::string> miVect;
        fila=node.node().child_value();
        std::istringstream in(fila);
        std::copy(std::istream_iterator<std::string>(in),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(miVect));
    	std:: string linea, strtile;
        int index = 0;
        for (index = 0; index < (int)miVect.size(); ++index) {
        	std:: string type, tileId;
        	if(miVect[index] != "1"){
        		type = TILE_STRING_TYPE_NORMAL;
        		tileId = miVect[index];
        	} else {
        		type = TILE_STRING_TYPE_BLOCK;
        		tileId = miVect[index];
        	}

        	strtile += " "+tileId+":"+type;
		}
        linea = strtile;
        salida << linea;
        salida << '\n';
    }
}

int main(int argc , char* argv[]){
    std::cout << "Ejemplo de creacion de archivo XML" << std::endl;
    crearArchivoXML();
    std::cout << "Ejemplo de carga de archivo XML" << std::endl;
    cargarArchivoXML();
    return 0;
}

